<?php

namespace App\Http\Controllers\Api;

use App\Enums\ArticleType;
use App\Http\Controllers\Controller;
use App\Http\Requests\ArticleRequest;
use App\Http\Resources\ArticleResource;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ArticleController extends Controller
{

    //常见问题
    public function index(Request $request)
    {
        $article = Article::query()->where('type', ArticleType::QUESTION)->show()->orderBy('sort')->orderByDesc('id')->paginate($request->get('limit', 10));
        return successReturn([
            'list' => ArticleResource::collection($article),
            'total' => $article->total(),
            'last_page' => $article->lastPage()
        ]);
    }


    public function show(Article $article)
    {
        if ($article->show) {
            return successReturn(new ArticleResource($article));
        }
        return successReturn();
    }


    //非常见问题类型的文章
    public function article(ArticleRequest $request)
    {
        return successReturn(new ArticleResource(Article::query()->where('type', $request->get('type', ArticleType::ABOUT))->show()->latest()->first()));
    }
}
